set rmsg on
set more off

************************************************************************
*
*	NOTE:		This do-file uses as input the final regression sample
*				and estimates the cash flow sensitivity of cash using
*				alterantive estimators.
*
*************************************************************************

capture program drop _all
clear
clear matrix


* Define paths

	global 	data	"..."
			
	
*************************************************************************
**** Beginn: Regressions using Erickson, Jiang, and Whited estimator ****
*************************************************************************	

	use "$data/GMMSV_reg_sample.dta", clear

	global d mbl1 cf5_lagat l_al1 
 
 **************************************************
 ** Define the volatility ration
	g vol_ratio = sigma_P/sigma_A
	tabstat vol_ratio, stats(n mean sd p25 p50 p75)

	**** Analysis for above median
	_pctile vol_ratio, percentiles(50)
 
	scalar top_vol = r(r1)


 * 1. Rho thresholds -0.02, 0.02 
 xtewreg cash_savings_at $d  if rho < -0.02 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d  if rho > 0.02 & vol_ratio > top_vol, maxdeg(4) mis(1) 
  
 * 2. Rho thresholds -0.03, 0.03
 xtewreg cash_savings_at $d  if rho < -0.03 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d  if rho >  0.03 & vol_ratio > top_vol, maxdeg(4) mis(1) 
 
 **************************************************************************************************************
 
 **** Analysis for above 60%
 _pctile vol_ratio, percentiles(60)
 
 scalar top_vol = r(r1)

 * 1. Rho thresholds -0.02, 0.02 
 xtewreg cash_savings_at $d  if rho < -0.02 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d  if rho > 0.02 & vol_ratio > top_vol, maxdeg(4) mis(1) 
 
 * 2. Rho thresholds -0.03, 0.03
 xtewreg cash_savings_at $d  if rho < -0.03 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d  if rho >  0.03 & vol_ratio > top_vol, maxdeg(4) mis(1) 
 
  **************************************************************************************************************
 
  **** Analysis for above 70%
  _pctile vol_ratio, percentiles(70)
 
  scalar top_vol = r(r1)
 
 * 1. Rho thresholds -0.02, 0.02 
 xtewreg cash_savings_at $d if rho < -0.02 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d if rho > 0.02 & vol_ratio > top_vol, maxdeg(4) mis(1) 
 
 * 2. Rho thresholds -0.03, 0.03
 xtewreg cash_savings_at $d if rho < -0.03 & vol_ratio > top_vol, maxdeg(4) mis(1)
 xtewreg cash_savings_at $d if rho >  0.03 & vol_ratio > top_vol, maxdeg(4) mis(1) 
 	
*************************************************************************
**** End: Regressions using Erickson, Jiang, and Whited estimator ****
*************************************************************************
	
*************************************************************************
**** Beginn: Regressions using Arellano and Bond estimator ****
*************************************************************************		
	
	use "$data/GMMSV_reg_sample.dta", clear	
	
	
	xtset gvkey fyear

	global c l_al1 
 
**	Calculate volatilities ratio
	*g vol_ratio = sigma_P/sigA_alpha
	g vol_ratio = sigma_P/sigma_A
	tabstat vol_ratio, stats(n mean sd p25 p50 p75)

**	2.1 Set volatility ratio above median
	_pctile vol_ratio, percentiles(50)
	scalar top_vol = r(r1)

**	2.1.i Set rho thresholds to [-0.02, 0.02]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeffs, etc. 
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all2 = (coeff\secoeff\pcoeff\e(N))
	
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, stack
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all2 = (all2\coeff\secoeff\pcoeff\e(N))	
 
**	2.1.ii Set rho thresholds to [-0.03, 0.03]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.03 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, 
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all3 = (coeff\secoeff\pcoeff\e(N))	
	
	* Positive rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.03 & vol_ratio > top_vol, lags(2) vce(gmm)	
	* save coeff, stack
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all3 = (all3\coeff\secoeff\pcoeff\e(N))	
	
**	2.2 Set volatility ratio above median
	_pctile vol_ratio, percentiles(60)
	scalar top_vol = r(r1)

**	2.2.i Set rho thresholds to [-0.02, 0.02]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff,
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all22 = (coeff\secoeff\pcoeff\e(N))	
	
	* Positive rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, stack
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all22 = (all22\coeff\secoeff\pcoeff\e(N))
	matrix all2  = (all2,all22)
	
**	2.2.ii Set rho thresholds to [-0.03, 0.03]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.03 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, 
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all32 = (coeff\secoeff\pcoeff\e(N))
	
	* Positive rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.03 & vol_ratio > top_vol, lags(2) vce(gmm)	
	* save coeff, stack	
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all32 = (all32\coeff\secoeff\pcoeff\e(N))
	matrix all3  = (all3,all32)
	
**	2.3 Set volatility ratio above median
	_pctile vol_ratio, percentiles(70)
	scalar top_vol = r(r1)

**	2.3.i Set rho thresholds to [-0.02, 0.02]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff,
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all22 = (coeff\secoeff\pcoeff\e(N))	
	
	* Positive rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.02 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, stack
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all22 = (all22\coeff\secoeff\pcoeff\e(N))
	matrix all2  = (all2,all22)	
 
**	2.3.ii Set rho thresholds to [-0.03, 0.03]
	* Negative rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho < -0.03 & vol_ratio > top_vol, lags(2) vce(gmm)
	* save coeff, 
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all32 = (coeff\secoeff\pcoeff\e(N))	
	
	* Positive rho sample
	xtabond cash_savings_at cf5_lagat l_al1 mbl1 if rho >  0.03 & vol_ratio > top_vol, lags(2) vce(gmm)

	* save coeff, stack	
	scalar coeff   = _b[cf5_lagat]
	scalar secoeff = _se[cf5_lagat]
	scalar dof     = e(N) - 3
	scalar pcoeff  = 2*ttail(dof,abs(coeff/secoeff))
	
	matrix all32 = (all32\coeff\secoeff\pcoeff\e(N))
	matrix all3  = (all3,all32)	

matrix list all2
matrix list all3
	
*************************************************************************
**** End: Regressions using Arellano and Bond estimator ****
*************************************************************************	
	